﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySkin.Tools;
using MySkin.Enums;

namespace MySkin.View
{
    public partial class BaseForm : Form
    {
        public BaseForm():base()
        {
            InitializeComponent();
            InitForm();
        }
        //资源图片名称
        public const String IMG_MIN = "btn_min";
        public const String IMG_MAX = "btn_max";
        public const String IMG_RESTORE = "btn_restore";
        public const String IMG_CLOSE = "btn_close";
        public const String IMG_BG = "img_bg";
        public const String IMG_MENU = "btn_menu";
        public const String IMG_SKIN = "btn_skin";

        public Point FormLocation = new Point();
        //图片缓存
        private Bitmap closeMap = null;
        private Bitmap minMap = null;
        private Bitmap maxMap = null;
        private Bitmap restoreMap = null;
        private Bitmap menuMap = null;
        private Bitmap skinMap = null;

       // private ControlAutoSize controlAutoSize = new ControlAutoSize();
        protected virtual void InitForm()
        {
            closeMap = ResUtils.GetResAsImage(IMG_CLOSE);
            minMap = ResUtils.GetResAsImage(IMG_MIN);
            maxMap = ResUtils.GetResAsImage(IMG_MAX);
            restoreMap = ResUtils.GetResAsImage(IMG_RESTORE);
            menuMap = ResUtils.GetResAsImage(IMG_MENU);
            skinMap = ResUtils.GetResAsImage(IMG_SKIN);

            TipMain.SetToolTip(BtnClose, "关闭");
            TipMain.SetToolTip(BtnMin, "最小化");
            TipMain.SetToolTip(BtnMax, "最大化");
            TipMain.SetToolTip(btnMenu, "设置");
            TipMain.SetToolTip(btnSkin, "皮肤");
            if (this.WindowState == FormWindowState.Maximized)
            {
                TipMain.SetToolTip(BtnMax, "还原");
                BtnMax.BackgroundImage = restoreMap;
                this.BtnMax.Invalidate();
            }
        }

        //重写窗体消息，使窗体可用鼠标缩放大小。
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case Win32Message.WM_NCHITTEST:     //MouseMove/down/up Message.
                    ResizeForm(ref m);
                    break;  
                default:
                    base.WndProc(ref m);
                    break;
            }
        }

        private void ResizeForm(ref Message m)
        {
            Point vPoint = new Point((int)m.LParam & 0xFFFF, (int)m.LParam >> 16 & 0xFFFF);
            vPoint = PointToClient(vPoint);
            if (vPoint.X <= 5)
                if (vPoint.Y <= 5)
                {
                    m.Result = (IntPtr)Win32Message.HTTOPLEFT;
                }
                else if (vPoint.Y >= ClientSize.Height - 5)
                {
                    m.Result = (IntPtr)Win32Message.HTBOTTOMLEFT;
                }
                else
                    m.Result = (IntPtr)Win32Message.HTLEFT;
            else if (vPoint.X >= ClientSize.Width - 5)
                if (vPoint.Y <= 5)
                    m.Result = (IntPtr)Win32Message.HTTOPRIGHT;
                else if (vPoint.Y >= ClientSize.Height - 5)
                    m.Result = (IntPtr)Win32Message.HTBOTTOMRIGHT;
                else
                    m.Result = (IntPtr)Win32Message.HTRIGHT;
            else if (vPoint.Y <= 5)
                m.Result = (IntPtr)Win32Message.HTTOP;
            else if (vPoint.Y >= ClientSize.Height - 5)
                m.Result = (IntPtr)Win32Message.HTBOTTOM;
        }
        private void MoveForm(ref Message m)
        {
            m.Msg = Win32Message.WM_NCLBUTTONDOWN;                                  //更改消息为非客户区按下鼠标
            m.LParam = IntPtr.Zero;                             //默认值
            m.WParam = new IntPtr(2);      //鼠标放在标题栏内
            //base.WndProc(ref m); 
        }

        protected virtual void BtnWnd_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (sender == BtnClose)
                {
                   // if (ShowInTaskbar == true)
                   // {
                        this.Close();
                    //}
                    //notifyIcon1.Visible = true;
                }
                else if (sender == BtnMin)
                {
                    this.WindowState = FormWindowState.Minimized;
                }
                else if (sender == BtnMax)
                {
                    if (this.WindowState == FormWindowState.Maximized)
                    {
                        this.WindowState = FormWindowState.Normal;
                        TipMain.SetToolTip(BtnMax, "最大化");
                        this.BtnMax.BackgroundImage = maxMap;
                    }
                    else if (this.WindowState == FormWindowState.Normal)
                    {
                        this.WindowState = FormWindowState.Maximized;
                        TipMain.SetToolTip(BtnMax, "还原");
                        this.BtnMax.BackgroundImage = restoreMap;
                    }
                }

            }
        }

        protected virtual void BtnWnd_MouseDown(object sender, MouseEventArgs e)
        {
            Bitmap BackImage = null;
            if (sender == this.BtnClose)
            {
                BackImage = ResUtils.GetResWithState(IMG_CLOSE, ControlState.MouseDown);
            }
            else if (sender == this.BtnMin)
            {
                BackImage = ResUtils.GetResWithState(IMG_MIN, ControlState.MouseDown);
            }
            else if (sender == this.btnMenu)
            {
                BackImage = ResUtils.GetResWithState(IMG_MENU, ControlState.MouseDown);
            }
            else if (sender == this.btnSkin)
            {
                BackImage = ResUtils.GetResWithState(IMG_SKIN, ControlState.MouseDown);
            }
            else if (sender == this.BtnMax)
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    BackImage = ResUtils.GetResWithState(IMG_MAX, ControlState.MouseDown);
                }
                else if (this.WindowState == FormWindowState.Maximized)
                {
                    BackImage = ResUtils.GetResWithState(IMG_RESTORE, ControlState.MouseDown);
                }
            }
            else
            {
                return;
            }
            
            Control control = (Control)sender;
            control.BackgroundImage = BackImage;
            control.Invalidate();
        }

        protected virtual void BtnWnd_MouseEnter(object sender, EventArgs e)
        {
            Bitmap BackImage = null;
            if (sender == this.BtnClose)
            {
                BackImage = ResUtils.GetResWithState(IMG_CLOSE, ControlState.MouseOver);
            }
            else if (sender == this.BtnMin)
            {
                BackImage = ResUtils.GetResWithState(IMG_MIN, ControlState.MouseOver);
            }
            else if (sender == this.btnMenu)
            {
                BackImage = ResUtils.GetResWithState(IMG_MENU, ControlState.MouseOver);
            }
            else if (sender == this.btnSkin)
            {
                BackImage = ResUtils.GetResWithState(IMG_SKIN, ControlState.MouseOver);
            }
            else if (sender == this.BtnMax)
            {
                if (this.WindowState == FormWindowState.Normal)
                {
                    BackImage = ResUtils.GetResWithState(IMG_MAX, ControlState.MouseOver);
                }
                else if (this.WindowState == FormWindowState.Maximized)
                {
                    BackImage = ResUtils.GetResWithState(IMG_RESTORE, ControlState.MouseOver);
                }
            }
            else
            {
                return;
            }
            Control control = (Control)sender;
            control.BackgroundImage = BackImage;
            control.Invalidate();
        }

        private void BtnWnd_MouseLeaveOrUp(object sender, EventArgs e)
        {
            Bitmap BackImage = null;
            if (sender == this.BtnClose)
            {
                BackImage = closeMap;
            }
            else if (sender == this.BtnMin)
            {
                BackImage = minMap;
            }
            else if (sender == this.btnMenu)
            {
                BackImage = menuMap;
            }
            else if (sender == this.btnSkin)
            {
                BackImage = skinMap;
            }   
            else if (sender == this.BtnMax)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    BackImage = restoreMap;
                }
                else
                {
                    BackImage = maxMap;
                }
            }
            else
            {
                return;
            }
            Control control = (Control)sender;
            control.BackgroundImage = BackImage;
            control.Invalidate();    //控件无效的
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
                this.Show();
                this.WindowState = FormWindowState.Normal;
                this.ShowInTaskbar = true;
        }

        private void CloseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BaseForm_Resize(object sender, EventArgs e)
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            this.Refresh();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            FormLocation.X = e.X;
            FormLocation.Y = e.Y;
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Top = Cursor.Position.Y - FormLocation.Y ;
                this.Left = Cursor.Position.X - FormLocation.X;               
            }
        }

        private void BtnWnd_MouseLeaveOrUp(object sender, MouseEventArgs e)
        {

        }

    }
}
